from django.db import models
from django.contrib.auth.models import User

class Cluster(models.Model):
    """集群模型"""
    name = models.CharField(max_length=100, verbose_name='集群名称')
    description = models.TextField(blank=True, verbose_name='描述')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='创建者')

    class Meta:
        verbose_name = '集群'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return self.name

class Node(models.Model):
    """节点模型"""
    STATUS_CHOICES = [
        ('running', '运行中'),
        ('stopped', '已停止'),
        ('error', '错误'),
    ]

    name = models.CharField(max_length=100, verbose_name='节点名称')
    cluster = models.ForeignKey(Cluster, on_delete=models.CASCADE, related_name='nodes', verbose_name='所属集群')
    ip_address = models.GenericIPAddressField(verbose_name='IP地址')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='running', verbose_name='状态')
    cpu_cores = models.IntegerField(verbose_name='CPU核心数')
    memory_gb = models.IntegerField(verbose_name='内存(GB)')
    disk_gb = models.IntegerField(verbose_name='磁盘(GB)')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '节点'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} ({self.cluster.name})"

class Instance(models.Model):
    """实例模型"""
    STATUS_CHOICES = [
        ('running', '运行中'),
        ('stopped', '已停止'),
        ('error', '错误'),
    ]

    name = models.CharField(max_length=100, verbose_name='实例名称')
    node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name='instances', verbose_name='所属节点')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='running', verbose_name='状态')
    cpu_cores = models.IntegerField(verbose_name='CPU核心数')
    memory_gb = models.IntegerField(verbose_name='内存(GB)')
    disk_gb = models.IntegerField(verbose_name='磁盘(GB)')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='创建者')

    class Meta:
        verbose_name = '实例'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} ({self.node.name})"

class ResourceUsage(models.Model):
    """资源使用记录"""
    instance = models.ForeignKey(Instance, on_delete=models.CASCADE, related_name='usage_records', verbose_name='实例')
    cpu_usage = models.FloatField(verbose_name='CPU使用率')
    memory_usage = models.FloatField(verbose_name='内存使用率')
    disk_usage = models.FloatField(verbose_name='磁盘使用率')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='记录时间')

    class Meta:
        verbose_name = '资源使用记录'
        verbose_name_plural = verbose_name
        ordering = ['-timestamp']

    def __str__(self):
        return f"{self.instance.name} - {self.timestamp}"
